package com.dong.boot.controller.system.basic;

import com.dong.boot.model.Department;
import com.dong.boot.model.RespBean;
import com.dong.boot.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    public List<Department> getAllDepartment(){
        return departmentService.getAllDepartment();
    }

    @PutMapping("/")
    public RespBean addDepartment(@RequestBody Department department){
        if (departmentService.addDepartment(department) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @PostMapping("/")
    public RespBean deleteDepartment(@RequestBody Department department){
        if (departmentService.deleteDepartment(department) == 0){
            return RespBean.ok("删除成功");
        }else if (departmentService.deleteDepartment(department) == 1){
            return RespBean.error("该部门下有子部门,无法删除!");
        }
        return RespBean.error("删除失败");
    }
}
